/**
 * Copyright (c) 2010-2011 yangzhiqi, 
 *
 */
package com.biosan.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class IOUtil
{
    public static void close(InputStream stream)
    {
        if (stream != null)
        {
            try
            {
                stream.close();
            }
            catch (IOException e)
            {
            }
        }
    }

    public static void close(OutputStream stream)
    {
        if (stream != null)
        {
            try
            {
                stream.close();
            }
            catch (IOException e)
            {
            }
        }
    }

    public static void close(Reader reader)
    {
        if (reader != null)
        {
            try
            {
                reader.close();
            }
            catch (IOException e)
            {
            }
        }
    }

    public static void write(String file, String content) throws IOException
    {
        OutputStreamWriter bos = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
        bos.write(content);
        bos.close();
    }

    public static void appendline(String file, String content, boolean isappend,boolean isrn) throws IOException
    {
        FileWriter writer = new FileWriter(file, isappend);
        writer.write(content);
        if(isrn)
        {
            writer.write("\r\n");
        }
        writer.close();
    }

    public static void delFile(String file) throws IOException
    {
        File delfile = new File(file);
        delfile.delete();

    }

    public static String read(String file) throws IOException
    {
        StringBuffer content = new StringBuffer();
        char[] buff = new char[1024];
        InputStreamReader bis = new InputStreamReader(new FileInputStream(file), "UTF-8");

        int read = bis.read(buff);
        while (read != -1)
        {
            content.append(new String(buff, 0, read));
            read = bis.read(buff);
        }

        bis.close();

        return content.toString();
    }
    
    public static boolean  renameTofile(String oldfile,String newfile)
    {
        
        File oldFile = new File(oldfile);
        File newFile = new File(newfile);
        return oldFile.renameTo(newFile); 
        
    }
    
    public static ArrayList<String> getFileslist(String filePath,String endwith)
    {
        ArrayList<String> flist = new ArrayList<String>();
        File root = new File(filePath);
        File[] files = root.listFiles();
        for (File file : files)
        {
            if (file.isDirectory())
            {
                /*
                 * 递归调用
                 */
               // getFiles(file.getAbsolutePath());
               // filelist.add(file.getAbsolutePath());
               // System.out.println("显示" + filePath + "下所有子目录及其文件" + file.getAbsolutePath());
            }
            else
            {
                if (file.toString().endsWith(endwith))
                {
                    flist.add(file.getAbsolutePath());
                }
              //  System.out.println("显示" + filePath + "下所有子目录" + file.getAbsolutePath());
            }
        }
        return flist;
    }

  
    public static Map<Integer, String> readline(String file) throws FileNotFoundException, IOException
    {
        Map<Integer, String> map=new HashMap<Integer, String>();       
        File f = new File(file);
        FileReader r = new FileReader(f);
        BufferedReader pr = new BufferedReader(r);
        String temp = null;
        Integer key = 0;
        while ((temp = pr.readLine()) != null)
        { // readLine()方法为读取一行
                
            map.put(key, temp);
            key++;      
        }
        return map;
    }
}
